package com.csu.express.controller.app;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.csu.common.exception.WebException;
import com.csu.common.vo.ResultVo;
import com.csu.express.constants.ErrorCode;
import com.csu.express.pojo.CustomerPojo;
import com.csu.express.service.CommonService;
import com.csu.express.service.CustomerService;
import com.csu.express.service.OrderService;
import com.csu.express.vo.OrderResponseVo;
import com.wordnik.swagger.annotations.Api;
import com.wordnik.swagger.annotations.ApiOperation;
import com.wordnik.swagger.annotations.ApiParam;

/**
 * @项目名称：express
 * @类名称：CustomerController
 * @类描述：
 * @创建人：Tangc
 * @创建时间：2017年9月21日 下午3:33:14
 * @company:
 */
@Controller
@RequestMapping("/customer")
@Api(value = "customer", description = "客户APP服务接口")
public class CustomerController
{
    @Autowired
    private CustomerService customerService;

    @Autowired
    private CommonService commonService;

    @Autowired
    private OrderService orderService;

    @ResponseBody
    @ApiOperation(value = "客户注册", notes = "校验注册参数，成功后返回用户信息")
    @RequestMapping(value = "sign", method = RequestMethod.POST, produces = { "application/json; charset=utf-8" })
    public ResultVo<CustomerPojo> customerSign(HttpServletRequest request,
            @ApiParam(value = "登录密码") @RequestParam(value = "password", required = true) String password,
            @ApiParam(value = "电话号码") @RequestParam(value = "cellphone", required = true) String cellphone,
            @ApiParam(value = "名称") @RequestParam(value = "name", required = false) String name,
            @ApiParam(value = "验证码") @RequestParam(value = "authCode", required = true) String authCode)
            throws WebException
    {
        if (cellphone == null || password == null || "".equals(cellphone.trim()) || "".equals(password.trim()))
        {
            // 参数不能为空
            throw new WebException(ErrorCode.ParamNullCode, ErrorCode.ParamNullMessgae);
        }

        ResultVo<CustomerPojo> result = new ResultVo<CustomerPojo>();

        commonService.checkAuthCode(cellphone, authCode);

        // 登录操作
        CustomerPojo customer = customerService.sign(cellphone, password, name);

        result.setData(customer);
        return result;
    }

    @ResponseBody
    @ApiOperation(value = "客户登录", notes = "登录校验，成功后返回用户信息")
    @RequestMapping(value = "login", method = RequestMethod.POST, produces = { "application/json; charset=utf-8" })
    public ResultVo<CustomerPojo> customerLogin(HttpServletRequest request,
            @ApiParam(value = "登录密码") @RequestParam(value = "password", required = true) String password,
            @ApiParam(value = "电话号码") @RequestParam(value = "cellphone", required = true) String cellphone)
            throws WebException
    {
        if (cellphone == null)
        {
            // 微信id和电话号码不能同时为空
            throw new WebException(ErrorCode.LoginParamErrorCode, ErrorCode.LoginParamErrorMessgae);
        }
        if (password == null)
        {
            // 参数不能为空
            throw new WebException(ErrorCode.ParamNullCode, ErrorCode.ParamNullMessgae);
        }

        ResultVo<CustomerPojo> result = new ResultVo<CustomerPojo>();

        // 登录操作
        CustomerPojo customer = customerService.login(cellphone, password);

        result.setData(customer);
        return result;
    }

    @ResponseBody
    @ApiOperation(value = "查询订单列表", notes = "查询订单列表")
    @RequestMapping(value = "getOrderList/{customerId}/{queryStatus}/{begin}/{size}", method = RequestMethod.GET, produces = { "application/json; charset=utf-8" })
    public ResultVo<List<OrderResponseVo>> getOrderList(HttpServletRequest request,
            @ApiParam(value = "客户id") @PathVariable(value = "customerId") Integer customerId,
            @ApiParam(value = "查询指定状态：1全部；2取货中 ；3送货中 ；4已结束  ") @PathVariable(value = "queryStatus") Integer queryStatus,
            @ApiParam(value = "查询开始位置，从0开始") @PathVariable(value = "begin") Integer begin,
            @ApiParam(value = "查询条数") @PathVariable(value = "size") int size) throws WebException
    {
        ResultVo<List<OrderResponseVo>> result = new ResultVo<List<OrderResponseVo>>();
        List<OrderResponseVo> orderReps = orderService.getOrderListByCustomerId(customerId, begin, size,queryStatus);
        result.setData(orderReps);
        return result;
    }

}
